package com.atguigu.tingshu.album.config;

import io.minio.MinioClient;
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

/*
@Configuration
@ConfigurationProperties(prefix="minio") //读取节点
@Data
public class MinioConstantProperties  implements InitializingBean {
    public static String ENDPOINT_URL;
    public static String ACCESS_KEY;
    public static String SECRET_KEY;
    public static String BUCKET_NAME;


    private String endpointUrl;
    private String accessKey;
    private String secretKey;
    private String bucketName;

    //利用SpringBean的生命周期方法， 再容器初始化当前组件对象 设置属性值以后
    // 将成员属性值 赋值给静态属性

    //容器对象属性值设置以后 调用
    @Override
    public void afterPropertiesSet()  throws Exception{
        ENDPOINT_URL = this.endpointUrl;
        ACCESS_KEY = this.accessKey;
        SECRET_KEY = this.secretKey;
        BUCKET_NAME = this.bucketName;
    }

   // @Lazy
    @ConditionalOnBean(value = MinioConstantProperties.class)
    @Bean
    public MinioClient minioClient(){
        return minioClient().builder()
                .endpoint(MinioConstantProperties.ENDPOINT_URL)
                .credentials(MinioConstantProperties.ACCESS_KEY,
                        MinioConstantProperties.SECRET_KEY)
                .build();
    }
*/


@Configuration
@ConfigurationProperties(prefix="minio") //读取节点
@Data
public class MinioConstantProperties implements InitializingBean {
   //通过静态属性接收属性值.不写硬编码，后续方便修改，不用大改引用的代码
    public static String ENDPOINT_URL;
    public static String ACCESS_KEY;
    public static String SECRET_KEY;
    public static String BUCKET_NAME;

    private String endpointUrl;
    private String accessKey;
    private String secretKey;
    private String bucketName;
    //利用SpringBean的生命周期方法， 再容器初始化当前组件对象 设置属性值以后
    // 将成员属性值 赋值给静态属性

    //容器对象属性值设置以后 调用
    @Override
    public void afterPropertiesSet() throws Exception {
        ENDPOINT_URL = this.endpointUrl;
        ACCESS_KEY = this.accessKey;
        SECRET_KEY = this.secretKey;
        BUCKET_NAME = this.bucketName;

    }
    //    @Lazy //懒加载： 表示当前组件对象 在使用时才初始化
   /*
   * 表示只有当容器中存在 MinioConstantProperties 类型的Bean时，
   * 才会创建当前的 minioClient Bean.通过这种方式，确保了依赖关系的正确性，避免在缺少必要配置时创建Bean。
   * */
    @ConditionalOnBean(value= MinioConstantProperties.class)
    @Bean
    public MinioClient minioClient(){
        return MinioClient.builder()
                .endpoint(MinioConstantProperties
                        .ENDPOINT_URL)
                .credentials(MinioConstantProperties.ACCESS_KEY,
                        MinioConstantProperties.SECRET_KEY)
                .build();
    }




}
